﻿using System;

namespace Communergy.Common
{
    public static class EnergyConverters
    {
        private const int JOULES_IN_KWH = 3600000;

        public static decimal JoulesToKWh(this int joules)
        {
            decimal decimalJoules = Convert.ToDecimal(joules);
            decimal kwh = decimalJoules / JOULES_IN_KWH;
            return kwh;

        }
        public static int KWhToJoules(this decimal kWh)
        {
            decimal decimalJoules = Convert.ToDecimal(JOULES_IN_KWH);
            int joules = Convert.ToInt32(kWh * decimalJoules);
            return joules;
            
        }

        public static TimeSpan MonthTimeSpan(this DateTime dateTime)
        {
            int year = dateTime.Year;
            int month = dateTime.Month;
            int daysInMonth = DateTime.DaysInMonth(year, month);
            TimeSpan timeSpan = new TimeSpan(daysInMonth, 0, 0, 0);
            return timeSpan;
        }

        public static int WattsIntervalToJoules(this decimal watts, TimeSpan interval)
        {
            return Convert.ToInt32(watts * (decimal)interval.TotalSeconds);
        }
        public static EnergyInformation EnergyInformationFromData(this EnergyData energyData)
        {
            EnergyInformation energyInformation = new EnergyInformation(energyData);
            return energyInformation;
        }
        public static EnergyData EnergyDataFromInformation(EnergyInformation energyInformation)
        {
            EnergyData energyData = new EnergyData
            {
                ReadingDateTime = energyInformation.ReadingDateTime,
                Interval = energyInformation.Interval,
                AverageWatts = energyInformation.AverageWatts
            };
            return energyData;
        }
    }
}
